

import org.junit.Test
import org.openqa.selenium.firefox.FirefoxDriver

import static org.junit.Assert.assertTrue
import org.openqa.selenium.WebDriver
import org.junit.BeforeClass
import org.junit.AfterClass
import static org.hamcrest.MatcherAssert.assertThat
import static org.junit.internal.matchers.StringContains.containsString

class FirstTest {

    static WebDriver driver
    static PROTOCOL = "http"
    static HOST = "www.compendiumdev.co.uk"

    @BeforeClass
    static void createDriver()
    {
        driver = new FirefoxDriver()
    }

    @Test
    void navigateWithNavigateTo()
    {
        driver.navigate().to("http://compendiumdev.co.uk/selenium/search.php")
        assertTrue(driver.getTitle().startsWith("Selenium Simplified Search Engine"))
    }

    @Test
    void shouldNavigateToHtmlForm()
    {
        driver.navigate().to("http://compendiumdev.co.uk/selenium/basic_html_form.html")
        assertTrue(driver.getTitle().startsWith("HTML Form Elements"))
    }

    @Test
    void shouldUseForwardBack()
    {
        driver.navigate().to("http://compendiumdev.co.uk/selenium/basic_html_form.html")
        driver.navigate().to("http://compendiumdev.co.uk/selenium/basic_web_page.html")
        driver.navigate().back()
        assertTrue(driver.getTitle().startsWith("HTML Form Elements"))
        driver.navigate().forward()
        assertTrue(driver.getTitle().startsWith("Basic Web Page Title"))
    }

    @Test
    void shouldUseRefresh()
    {
        driver.navigate().to("http://compendiumdev.co.uk/selenium/refresh.php")
        def firstTitle = driver.getTitle()
        driver.navigate().refresh()
        def titleAfterRefresh = driver.getTitle()
        assertThat(firstTitle,!is(titleAfterRefresh))
    }

    @Test
    void someMoreNavigation()
    {
        def URL = new URL(PROTOCOL, HOST, "/selenium/basic_web_page.html")
        driver.navigate().to(URL)
        assertTrue(driver.getTitle().startsWith("Basic Web Page Title"))
        assertTrue(driver.getCurrentUrl().endsWith("basic_web_page.html"))
        assertThat(driver.getCurrentUrl(), containsString("basic_web_page.html"))
        assertTrue(driver.getPageSource().contains("A paragraph of text"))
    }

    @AfterClass
    static void quitDriver()
    {
        driver.quit()
    }

}

